//
//  YSMessageView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/7.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSMessageView.h"

#define RED_POINT_HEIGHT 12

@interface YSMessageView()
{
    UIImageView *_messageImage;
    UILabel *_numLabel;
}
@end
@implementation YSMessageView

- (instancetype)init{
    self = [super init];
    if (self) {
        UIImage *image = [UIImage imageNamed:@"icon_message_gray"];
        _messageImage = [[UIImageView alloc]initWithImage:image];
        [_messageImage setFrame:CGRectMake(0, RED_POINT_HEIGHT/2, image.size.width, image.size.height)];
        [self addSubview:_messageImage];
        
        _numLabel = [[UILabel alloc]init];
        [_numLabel setTextColor:[UIColor whiteColor]];
        [_numLabel setFont:[UIFont systemFontOfSize:8]];
        [_numLabel setTextAlignment:NSTextAlignmentCenter];
        [_numLabel setHidden:YES];
        
        [_numLabel.layer setCornerRadius:5];
        [_numLabel.layer setBorderColor:RGBColor(204, 204, 204).CGColor];
        [_numLabel.layer setBackgroundColor:[ThemeColor CGColor]];
        
        [self addSubview:_numLabel];
        [self setBounds:CGRectMake(0, 0, image.size.width, image.size.height+RED_POINT_HEIGHT)];
    }
    return self;
}

- (void)setNum:(NSUInteger)num{
    _num = num;
    
    NSString *numStr = [NSString stringWithFormat:@"%lu",(unsigned long)num];
    if(!num){
        [_numLabel setHidden:YES];
        return;
    }
    
    [_numLabel setHidden:NO];
    if(num>999){
        numStr = @"999+";
    }
    
    CGSize size = [numStr sizeWithFont:SYS_FONT(8)];
    CGFloat numWidth = size.width +6;
//    CGFloat numHeight = size.height +2;
    
    [_numLabel setFrame:CGRectMake(_messageImage.image.size.width-(numWidth)*0.5, 3, numWidth, RED_POINT_HEIGHT)];
    [self setBounds:CGRectMake(0, 0, _messageImage.image.size.width+(numWidth)*0.5, _messageImage.image.size.height+RED_POINT_HEIGHT)];
    [_numLabel setText:numStr];
}

@end
